package com.mock.action.services;

import java.util.ArrayList;
import java.util.List;

import com.mock.action.AbstractAction;
import com.mock.dao.ServiceDao;
import com.mock.dao.impl.ServiceDaoImpl;
import com.mock.model.DataTable;
import com.mock.model.Service;

public class ServiceAction extends AbstractAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = -8626173811548042907L;
	
	private ServiceDao serviceDao = new ServiceDaoImpl();
	private List<ArrayList<String>> aaData = new ArrayList<ArrayList<String>>();
	private String sEcho;
	private int iDisplayLength;
	private int iDisplayStart = 0;
	private int iTotalRecords;
	private Long iTotalDisplayRecords;
	private String sSearch_1;
	private int iSortCol_0;
	private String sSortDir_0;
	private String sSearch_4;
	
	public String execute() {
		return SUCCESS;
	}
	
	public String serviceTableAjax() {
		DataTable dt = new DataTable();
		dt.sEcho = sEcho = getRequest().getParameter("sEcho");
		dt.iDisplayStart = iDisplayStart = Integer.parseInt(getRequest().getParameter("iDisplayStart"));
		dt.iDisplayLength = iDisplayLength = Integer.parseInt(getRequest().getParameter("iDisplayLength"));
		dt.sSearch_1 = sSearch_1 = getRequest().getParameter("sSearch_1");
		dt.sSearch_4 = sSearch_4 = getRequest().getParameter("sSearch_4");
		dt.iSortCol_0 = iSortCol_0 = Integer.parseInt(getRequest().getParameter("iSortCol_0"));
		dt.sSortDir_0 = sSortDir_0 = getRequest().getParameter("sSortDir_0");
		iTotalDisplayRecords = serviceDao.getTotalRecordByQuery(dt);
		iTotalRecords = serviceDao.getRowCount(Service.class);
		List<Service> services = serviceDao.getServiceByQuery(dt);
		for (Service service : services) {
			ArrayList<String> newArray = new ArrayList<String>();
			newArray.add("<input type='radio' id='" + Long.toString(service.getId()) + "'/>");
			newArray.add(service.getName());
			newArray.add(service.getShortDescription());
			newArray.add(service.getServiceType().getName());
			newArray.add(service.getContact().getFirstname() + " " + service.getContact().getSurname());
			if (service.isActive()) {
				newArray.add("Yes");				
			} else {				
				newArray.add("No");				
			}
			newArray.add(Long.toString(service.getId()));
			aaData.add(newArray);
		}
		return SUCCESS;
	}

	public List<ArrayList<String>> getAaData() {
		return aaData;
	}

	public void setAaData(List<ArrayList<String>> aaData) {
		this.aaData = aaData;
	}

	public String getsEcho() {
		return sEcho;
	}

	public void setsEcho(String sEcho) {
		this.sEcho = sEcho;
	}

	public int getiDisplayLength() {
		return iDisplayLength;
	}

	public void setiDisplayLength(int iDisplayLength) {
		this.iDisplayLength = iDisplayLength;
	}

	public int getiDisplayStart() {
		return iDisplayStart;
	}

	public void setiDisplayStart(int iDisplayStart) {
		this.iDisplayStart = iDisplayStart;
	}

	public int getiTotalRecords() {
		return iTotalRecords;
	}

	public void setiTotalRecords(int iTotalRecords) {
		this.iTotalRecords = iTotalRecords;
	}

	public Long getiTotalDisplayRecords() {
		return iTotalDisplayRecords;
	}

	public void setiTotalDisplayRecords(Long iTotalDisplayRecords) {
		this.iTotalDisplayRecords = iTotalDisplayRecords;
	}

	public String getsSearch_1() {
		return sSearch_1;
	}

	public void setsSearch_1(String sSearch_1) {
		this.sSearch_1 = sSearch_1;
	}

	public int getiSortCol_0() {
		return iSortCol_0;
	}

	public void setiSortCol_0(int iSortCol_0) {
		this.iSortCol_0 = iSortCol_0;
	}

	public String getsSortDir_0() {
		return sSortDir_0;
	}

	public void setsSortDir_0(String sSortDir_0) {
		this.sSortDir_0 = sSortDir_0;
	}

	public String getsSearch_4() {
		return sSearch_4;
	}

	public void setsSearch_4(String sSearch_4) {
		this.sSearch_4 = sSearch_4;
	}
}
